<script setup lang="ts">
import type { KnowledgeList, KnowledgeParams, KnowledgeType } from '@/types/consult'
import { getKnowledgePage } from '@/services/consult'
import { ref } from 'vue'
import KnowledgeCard from './KnowledgeCard.vue'

const props = defineProps<{
  type: KnowledgeType
}>()
// 实现加载更多效果
const loading = ref(false)
const finished = ref(false)
const list = ref<KnowledgeList>([])

const params = ref<KnowledgeParams>({
  type: props.type,
  current: 1,
  pageSize: 20
})
const onLoad = async () => {
  const res = await getKnowledgePage(params.value)
  list.value.push(...res.data.rows)
  loading.value = false //追加完数据 关闭loading
  // 是否加载完成 当前页 大于等于总页数就是获取完了
  if (params.value.current >= res.data.pageTotal) {
    finished.value = true
  } else {
    params.value.current++
  }
}
</script>

<template>
  <div class="knowledge-list">
    <van-list v-model:loading="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
      <knowledge-card v-for="(item, i) in list" :key="i" :item="item"></knowledge-card>
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
